package com.longrise.android.byjk.plugins.tabfirst;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.glide.RoundCornersTransformation;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.UmengStatisticsUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by callxkj on 2017/2/23.
 * 首页item中保险头条的这种adapter
 * recyclerview的图片底部有文字
 */

public class HomeRcvItem2Adapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    private List<EntityBean> mListBeans = new ArrayList<>();
    private OnItem2ClickListener mItem2ClickListener;

    public HomeRcvItem2Adapter() {
        super(R.layout.item_homercv2, null);
    }

    public void refreshData(EntityBean[] beans) {
        if (beans == null) {
            return;
        }
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        setNewData(mListBeans);
    }

    /**
     * 设置图片的点击事件
     */
    public void setOnItem2ClickListener(OnItem2ClickListener listener) {
        this.mItem2ClickListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, EntityBean item) {
        final int position = helper.getLayoutPosition();
        View convertView = helper.getConvertView();
        TextView tv = (TextView) convertView.findViewById(R.id.item_homercv2_tv);
        ImageView iv = (ImageView) convertView.findViewById(R.id.item_homercv2_iv);

        EntityBean entityBean = mListBeans.get(position);
        String picpath = entityBean.getString("picpath");
        final String linkedurl = entityBean.getString("linkedurl");
        final String linkedtype = entityBean.getString("linkedtype");
        final String text = entityBean.getString("text");

        RoundCornersTransformation transformation =
                new RoundCornersTransformation(mContext,
                        AppUtil.dip2px(4),
                        RoundCornersTransformation.CornerType.ALL);

        Glide.with(AppUtil.getContext())
                .load(picpath)
                .placeholder(R.drawable.by_home_tuijian)
                .bitmapTransform(transformation)
                .into(iv);
        tv.setText(text);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItem2ClickListener != null) {
                    mItem2ClickListener.onClick(linkedurl, text, linkedtype);
                    UmengStatisticsUtil.onEvent("news_recommend" + (helper.getLayoutPosition() + 1));
                }

            }
        });
    }

    interface OnItem2ClickListener {
        void onClick(String url, String title, String linkedtype);
    }
}
